﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Models.PurchaseOrderModel;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManageSystemApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    public class PurchaseOrderController : Controller
    {
        readonly IPurchaseOrderServices _purchaseOrder;
        readonly IUserServices _user;
        public PurchaseOrderController(IPurchaseOrderServices purchaseOrder, IUserServices user)
        {
            _purchaseOrder = purchaseOrder;
            _user = user;
        }

        /// <summary>
        /// 手机端提交单据管理
        /// </summary>
        [HttpPost, Route("Create")]
        public ApiResult Create([FromHeader(Name = "Authorization")] string auth, AddPurchaseOrderDto dto)
        {
            if (dto == null
                || dto.Amount <= 0
                || string.IsNullOrWhiteSpace(dto.Plan))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            // 获取当前UserId
            if (!_user.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            dto.Buyer = userId;
            bool b = _purchaseOrder.Create(dto, userId);
            return ApiResultStatus.Create(b, ApiResultCodes.OPERATION_FAILED);
        }

        /// <summary>
        /// Pc端显示报销列表
        /// </summary>
        [HttpPost, Route("Query")]
        public ApiResult Query(PageBo<PurchaseOrderParams> pagebo)
        {
            if (pagebo == null || pagebo.Param == null
                || pagebo.Param.StartTime == default(DateTime)
                || pagebo.Param.EndTime == default(DateTime))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (pagebo.Size <= 0)
            {
                pagebo.Size = 10;
            }
            if (pagebo.Page <= 0)
            {
                pagebo.Page = 1;
            }
            if (pagebo.Param == null)
            {
                pagebo.Param = new PurchaseOrderParams();
            }
            return ApiResultStatus.Success(_purchaseOrder.Get(pagebo));
        }

        [HttpPost, Route("QueryByUser")]
        public ApiResult QueryByUser([FromHeader(Name = "Authorization")] string auth, PageBo<PurchaseOrderParams> pageBo)
        {
            if (pageBo == null) 
            {
                pageBo = new PageBo<PurchaseOrderParams>();
            }
            if (pageBo.Size <= 0) 
            {
                pageBo.Size = 10;
            }
            if (pageBo.Page <= 0) 
            {
                pageBo.Page = 1;
            }
            if (pageBo.Param == null) 
            {
                pageBo.Param = new PurchaseOrderParams();
            }
            // 获取当前UserId
            if (!_user.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            pageBo.Param.Buyer = userId;
            return ApiResultStatus.Success(_purchaseOrder.Get(pageBo));
        }
    }
}
